﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace AB.Common
{
    /// <summary>
    /// MD5加密
    /// </summary>
    public class ABMD5
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="data">加密数据</param>
        /// <returns>MD5大写加密串</returns>
        public static string Encrypt(string data)
        {
            string s = string.Empty;
            byte[] buffer = Encoding.UTF8.GetBytes(data);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] cryMD5 = md5.ComputeHash(buffer);
            for (int i = 0; i < cryMD5.Length; i++)
            {
                s += cryMD5[i].ToString("X2");
            }
            return s;
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Encrypt(Stream data)
        {
            string s = string.Empty;
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] cryMD5 = md5.ComputeHash(data);
            for (int i = 0; i < cryMD5.Length; i++)
            {
                s += cryMD5[i].ToString("X2");
            }
            return s;
        }
        /// <summary>
        /// MD5对比
        /// </summary>
        /// <param name="data"></param>
        /// <param name="md5"></param>
        /// <returns></returns>
        public static bool MD5Contrast(string data,string md5)
        {
            if (!string.IsNullOrEmpty(data) &&!string.IsNullOrEmpty(md5))
            {
                string _md5 = Encrypt(data);
                return _md5 == md5.ToUpper();
            }
            return false;
        }
    }
    /// <summary>
    /// DES加解密
    /// </summary>
    public class ABDES
    {
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="DecryptStr">密文</param>
        /// <param name="Key">密钥</param>
        /// <param name="IV">向量</param>
        /// <returns>解密字符串，解密失败返回密文</returns>
        public static string Decrypt(string DecryptStr, string Key, string IV)
        {

            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Key = Encoding.UTF8.GetBytes(Key);
                des.IV = Encoding.UTF8.GetBytes(IV);
                string result = string.Empty;
                MemoryStream ms = new MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    byte[] inputByteArray = Convert.FromBase64String(DecryptStr);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    result = Encoding.UTF8.GetString(ms.ToArray());
                }
                return result;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="data">加密数据</param>
        /// <param name="Key">密钥</param>
        /// <param name="IV">向量</param>
        /// <returns>加密字符串，加密失败返回原文</returns>
        public static string Encrypt(string data, string Key, string IV)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Key = Encoding.UTF8.GetBytes(Key);
                des.IV = Encoding.UTF8.GetBytes(IV);
                string result = string.Empty;
                MemoryStream ms = new MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(data);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    result = Convert.ToBase64String(ms.ToArray());
                }
                return result;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
    }
    /// <summary>
    /// AES加解密
    /// </summary>
    public class ABAES
    {
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="DecryptStr">密文</param>
        /// <param name="Key">密钥</param>
        /// <param name="IV">向量</param>
        /// <returns>解密字符串，解密失败返回密文</returns>
        public static string Decrypt(string DecryptStr, string Key, string IV)
        {
            try
            {
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                aes.Key = Encoding.UTF8.GetBytes(Key);
                aes.IV = Encoding.UTF8.GetBytes(IV);
                string result = string.Empty;
                MemoryStream ms = new MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    byte[] inputByteArray = Convert.FromBase64String(DecryptStr);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    result = Encoding.UTF8.GetString(ms.ToArray());
                }
                return result;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="data">加密数据</param>
        /// <param name="Key">密钥</param>
        /// <param name="IV">向量</param>
        /// <returns>加密字符串，加密失败返回原文</returns>
        public static string Encrypt(string data, string Key, string IV)
        {
            try
            {
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                aes.Key = Encoding.UTF8.GetBytes(Key);
                aes.IV = Encoding.UTF8.GetBytes(IV);
                string result = string.Empty;
                MemoryStream ms = new MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(data);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    result = Convert.ToBase64String(ms.ToArray());
                }
                return result;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// AES解密ECB
        /// </summary>
        /// <param name="DecryptStr">密文</param>
        /// <param name="Key">密钥</param>
        /// <returns>解密字符串，解密失败返回密文</returns>
        public static string DecryptECB(string DecryptStr, string Key)
        {
            try
            {
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                aes.Key = Encoding.UTF8.GetBytes(Key);
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;
                string result = string.Empty;
                MemoryStream ms = new MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    byte[] inputByteArray = Convert.FromBase64String(DecryptStr);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    result = Encoding.UTF8.GetString(ms.ToArray());
                }
                return result;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// AES加密ECB
        /// </summary>
        /// <param name="data">加密数据</param>
        /// <param name="Key">密钥</param>
        /// <returns>加密字符串，加密失败返回原文</returns>
        public static string EncryptECB(string data, string Key)
        {
            try
            {
                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                aes.Key = Encoding.UTF8.GetBytes(Key);
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;
                string result = string.Empty;
                MemoryStream ms = new MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(data);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    result = Convert.ToBase64String(ms.ToArray());
                }
                return result;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
    }
    /// <summary>
    /// ABSecurity
    /// </summary>
    public class ABSecurity
    {
        static string defaultkey = "alWx78$8CxfJyQV*vF&f@IV!MlsrLUa8QCWL*xF*ON7C3*vd46lrVctLR@GmwAgVsQ!lJoaDTXKqf*KX8%uiPnvQim8!P*ADocv";
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static string Encrypt(string data, string Key)
        {
            string keyMD5 = ABMD5.Encrypt(Key).ToLower();
            string base64data = Convert.ToBase64String(Encoding.UTF8.GetBytes(data));
            string encryptStr = ABAES.Encrypt(base64data, keyMD5, ABMD5.Encrypt(keyMD5).ToLower());
            return encryptStr;
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Encrypt(string data)
        {
            return Encrypt(data, defaultkey);
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static string Decrypt(string data, string Key)
        {
            string keyMD5 = ABMD5.Encrypt(Key).ToLower();
            string base64data = ABAES.Decrypt(data, keyMD5, ABMD5.Encrypt(keyMD5).ToLower());
            string decryptStr = Encoding.UTF8.GetString(Convert.FromBase64String(base64data));
            return decryptStr;
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Decrypt(string data)
        {
            return Decrypt(data, defaultkey);
        }
    }
}
